package com.dlc.shop.user.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.bean.model.User;
import com.dlc.shop.bean.param.UserUpdateParam;
import com.dlc.shop.common.annotation.SysLog;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.enums.UserLevelType;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.service.UserService;
import com.dlc.shop.user.common.dto.UserLevelDto;
import com.dlc.shop.user.common.enums.GrowthLogSourceEnum;
import com.dlc.shop.user.common.model.UserLevel;
import com.dlc.shop.user.common.service.UserLevelLangService;
import com.dlc.shop.user.common.service.UserLevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 会员等级
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Tag(name = "会员等级")
@RestController
@AllArgsConstructor
@RequestMapping("/user/userLevel" )
public class UserLevelController {

    private final UserLevelService userLevelService;
    private final UserService userService;
    private final UserLevelLangService userLevelLangService;

    @SysLog("查看会员等级" )
    @GetMapping("/list" )
    @Operation(summary = "获取用户等级列表" , description = "获取用户等级列表")
    @PreAuthorize("@pms.hasPermission('user:userLevel:list')" )
    public ServerResponseEntity<List<UserLevelDto>> listUserLevel(Integer userLevelType) {
        return ServerResponseEntity.success(userLevelService.listUserLevelsByUserLevelType(userLevelType));
    }

    @GetMapping("/info/{id}" )
    @Operation(summary = "查询会员等级" , description = "通过id查询会员等级")
    @Parameter(name = "id", description = "会员等级id" )
    @PreAuthorize("@pms.hasPermission('user:userLevel:info')" )
    public ServerResponseEntity<UserLevel> getById(@PathVariable("id") Long id) {
        UserLevel userLevel = userLevelService.getUserLevelById(id);
        return ServerResponseEntity.success(userLevel);
    }

    @SysLog("新增会员等级" )
    @PostMapping
    @Operation(summary = "新增会员等级" , description = "新增会员等级")
    @PreAuthorize("@pms.hasPermission('user:userLevel:save')" )
    public ServerResponseEntity<String> save(@RequestBody @Valid UserLevel userLevel) {
        userLevel.setShopId(Constant.PLATFORM_SHOP_ID);
        userLevel.setLevelStyle(0);
        userLevel.setRecruitStatus(1);
        userLevelService.updateUserLevelList(userLevel);
        return ServerResponseEntity.success(userLevel.getId() + "");
    }

    @SysLog("修改会员等级" )
    @PutMapping
    @Operation(summary = "修改会员等级" , description = "修改会员等级")
    @PreAuthorize("@pms.hasPermission('user:userLevel:update')" )
    public ServerResponseEntity<Void> updateById(@RequestBody @Valid UserLevel userLevel) {
        userLevel.setShopId(Constant.PLATFORM_SHOP_ID);
        userLevelService.updateUserLevelList(userLevel);
        return ServerResponseEntity.success();
    }

    @SysLog("删除会员等级" )
    @DeleteMapping
    @Operation(summary = "删除会员等级" , description = "删除会员等级")
    @PreAuthorize("@pms.hasPermission('user:userLevel:delete')" )
    public ServerResponseEntity<Boolean> removeById(@RequestBody @Valid UserLevel userLevel) {
        userLevel.setShopId(Constant.PLATFORM_SHOP_ID);
        if (Objects.equals(userLevel.getLevelType(), UserLevelType.VIP.value())) {
            UserLevel userLevelDb = userLevelService.getUserLevelById(userLevel.getId());
            long userNum = userService.count(new LambdaQueryWrapper<User>().eq(User::getLevel, userLevelDb.getLevel()).eq(User::getLevelType, userLevelDb.getLevelType()));
            if (userNum > 0) {
                // 如果有用户是该付费会员等级，则不允许删除该等级
                throw new YamiShopBindException("yami.level.delete.check");
            }
        }
        return ServerResponseEntity.success(userLevelService.deleteUserLevel(userLevel));
    }

    @PutMapping("/updateUserLevel")
    @Operation(summary = "更新用户会员等级" , description = "更新用户会员等级")
    @PreAuthorize("@pms.hasPermission('user:userLevel:updateUserLevel')" )
    public ServerResponseEntity<Boolean> updateUserLevel(@RequestBody @Valid UserLevel userLevel) {
        return ServerResponseEntity.success(userLevelService.updateUserLevel(userLevel));
    }

    @PutMapping("/updateGrowth")
    @Operation(summary = "平台批量修改会员成长值" , description = "平台批量修改会员成长值")
    @PreAuthorize("@pms.hasPermission('user:userLevel:updateGrowth')" )
    public ServerResponseEntity<Boolean> batchUserGrowth(@RequestBody UserUpdateParam param) {
        param.setGrowthSource(GrowthLogSourceEnum.SYSTEM.value());
        return ServerResponseEntity.success(userLevelService.batchUpdateGrowth(param));
    }

    @PutMapping("/updateScore")
    @Operation(summary = "平台批量修改会员积分" , description = "平台批量修改会员积分")
    @PreAuthorize("@pms.hasPermission('user:userLevel:updateScore')" )
    public ServerResponseEntity<Boolean> batchUserScore(@RequestBody UserUpdateParam param) {
        return ServerResponseEntity.success(userLevelService.batchUpdateScore(param));
    }
    @PutMapping("/recruitStatus")
    @Operation(summary = "付费会员，是否可以招募会员；1可以招募，0停止招募", description = "付费会员，是否可以招募会员；1可以招募，0停止招募")
    public ServerResponseEntity<Void> updateRecruitStatus(@Valid @RequestBody UserLevel userLevelDTO) {
        Long userLevelId = userLevelDTO.getId();
        if (!Objects.equals(userLevelDTO.getLevelType(), UserLevelType.VIP.value())) {
            throw new YamiShopBindException("yami.update.level.recruit.status");
        }
        userLevelService.updateRecruitStatus(userLevelDTO);
        return ServerResponseEntity.success();
    }
}
